package pl.pelcra.cesar.spider.datadownloader;

import java.sql.Connection;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;

import pl.pelcra.cesar.data.interfaces.ICommonData;
import pl.pelcra.cesar.database.pool.DBConnectionPooler;
import pl.pelcra.cesar.tools.Tools;
import pl.pelcra.cesar.webrecorder.database.tables.DBAttachment;
import pl.pelcra.cesar.webrecorder.database.tables.DBAttachmentDAO;
import pl.pelcra.cesar.webrecorder.database.tables.DBWebRecord_Attachment_Xref;
import pl.pelcra.cesar.webrecorder.database.tables.DBWebRecord_Attachment_XrefDAO;

/**
 * Class extends Thread, download bytes of link and return DBAttachment object
 * 
 * @author margielewski
 * 
 */
public class DataDownloader extends Thread {

	private ICommonData<Map.Entry<Integer, String>> attachmentThreadData = null;
	private final Connection connection;
	private int i = 0;
	private final Logger logger = Logger.getLogger(DataDownloader.class);

	public DataDownloader(
			ICommonData<Map.Entry<Integer, String>> attachmentThreadData) {
		this.connection = DBConnectionPooler.INSTANCE
				.getCPWebRecordConnection();
		this.attachmentThreadData = attachmentThreadData;
	}

	/**
	 * function donwload data and set them to DBAttachment object
	 * 
	 * @param urlAttachment
	 *            - url link of attachment
	 * @return - return DBAttachment object
	 */
	private DBAttachment getDBAttachment(String urlAttachment) {
		DBAttachment dbAttachment = new DBAttachment();
		dbAttachment.setUrlAttachment(urlAttachment);
		dbAttachment.setContent(Tools.downloadData(urlAttachment));
		dbAttachment.setMetaData("PDF");
		dbAttachment.setDownloadDate(new Date());
		return dbAttachment;
	}

	/**
	 * overridden method from interface Runnable
	 */
	@Override
	public void run() {
		this.logger.info("DataDownloader starts working");
		DBAttachmentDAO attachmentDAO = new DBAttachmentDAO(this.connection);
		DBWebRecord_Attachment_XrefDAO xrefDAO = new DBWebRecord_Attachment_XrefDAO(
				this.connection);

		while (!this.attachmentThreadData.isEndPutting()) {
			Map.Entry<Integer, String> mapIdAttachment = this.attachmentThreadData
					.get();

			if (mapIdAttachment != null && mapIdAttachment.getKey() != 0
					&& !mapIdAttachment.getValue().isEmpty()) {
				int idWebRecord = mapIdAttachment.getKey();
				String urlAttachment = mapIdAttachment.getValue();

				int idAttachment = attachmentDAO
						.getIdDBAttachment(urlAttachment);

				// no exists attachment in table attachment
				if (idAttachment == 0) {
					DBAttachment dbAttachment = getDBAttachment(urlAttachment);
					idAttachment = attachmentDAO
							.insertDBAttachment(dbAttachment);
				}

				if (idWebRecord > 0 && idAttachment > 0) {
					DBWebRecord_Attachment_Xref xref = new DBWebRecord_Attachment_Xref();
					xref.setfK_WebRecord(idWebRecord);
					xref.setfK_Attachment(idAttachment);
					idAttachment = xrefDAO.insertXREFWebRecord_Attachment(xref);
					if (idAttachment > 0) {
						this.logger.info(String.format(
								"Insert %s attachment %s", this.i++,
								urlAttachment));
					} else {
						this.logger.info(String.format(
								"Error with attachment %s", urlAttachment));
					}
				}

			}
		}
		this.logger.info("DataDownloader ends working");
	}
}
